package jehc.cloud.gen.service.impl;

import jehc.cloud.common.base.BaseService;
import jehc.cloud.common.util.ExceptionUtil;
import jehc.cloud.common.util.dbtools.DBMSMetaUtil;
import jehc.cloud.common.util.dbtools.DbInfo;
import jehc.cloud.common.util.dbtools.MapUtil;
import jehc.cloud.gen.dao.XtGeneratorDao;
import jehc.cloud.gen.model.XtDbinfo;
import jehc.cloud.gen.model.XtGenerator;
import jehc.cloud.gen.model.XtGeneratorTableColumn;
import jehc.cloud.gen.service.XtDbinfoService;
import jehc.cloud.gen.service.XtGeneratorService;
import jehc.cloud.gen.service.generator.Gutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * @Desc 代码生成器
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service("xtGeneratorService")
public class XtGeneratorServiceImpl extends BaseService implements XtGeneratorService {
	@Autowired
	private XtGeneratorDao xtGeneratorDao;

	@Autowired
	private XtDbinfoService xtDbinfoService;
	/**
	 * 查询所有生成信息并分页
	 * @param condition
	 * @return
	 */
	public List<XtGenerator> getXtGeneratorListByCondition(Map<String, Object> condition){
		try {
			return xtGeneratorDao.getXtGeneratorListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
	/**
	 * 生成代码
	 * @param xtGenerator
	 */
	public int addXtGenerator(XtGenerator xtGenerator){
		Gutil gutil = new Gutil();
		int result = 0;
		try {
			String xt_dbinfo_id = xtGenerator.getXt_dbinfo_id();
			XtDbinfo xtDbinfo = xtDbinfoService.getXtDbinfoById(xt_dbinfo_id);
			String table_name = xtGenerator.getXt_generator_tbname();
			String comment = xtGenerator.getXt_generator_tbcomment();
			List<XtGeneratorTableColumn> xt_Generator_Table_ColumnList = new ArrayList<XtGeneratorTableColumn>();
			DbInfo info = convert(xtDbinfo);
			info.setTableName(xtGenerator.getXt_generator_tbname());
			DbInfo dbInfo = DBMSMetaUtil.excuteDB(info);
			List<Map<String, Object>> columns = dbInfo.getColumns();
			List<Map<String, Object>> columnsPrimary = MapUtil.convertKeyList2LowerCase(dbInfo.getColumnsPrimary());
			String pk=null;
			for(int i = 0; i < columnsPrimary.size(); i++){
				Map<String, Object> map = columnsPrimary.get(i);
				pk = map.get("column_name").toString();
				break;
			}
			if(!columns.isEmpty() && columns.size()>0){
				for(int i = 0; i < columns.size(); i++){
					Map<String, Object> map = columns.get(i);
					XtGeneratorTableColumn xt_Generator_Table_Column = new XtGeneratorTableColumn();
					xt_Generator_Table_Column.setCOLUMN_NAME(map.get("COLUMN_NAME").toString());
					Object remarksObj = map.get("REMARKS");
					if(null != remarksObj){
						String remarks = remarksObj.toString();
						try {
							xt_Generator_Table_Column.setCOLUMN_COMMENT(new String(remarks.getBytes(),"UTF-8"));//解决乱码问题
						}catch (Exception e){

						}
					}

					/**
					xt_Generator_Table_Column.setDATA_TYPE(map.get("DATA_TYPE").toString());
					**/
					xt_Generator_Table_Column.setDATA_TYPE(map.get("TYPE_NAME").toString());
					xt_Generator_Table_Column.setIS_NULLABLE(map.get("IS_NULLABLE").toString());
					xt_Generator_Table_Column.setCHARACTER_MAXIMUM_LENGTH(map.get("COLUMN_SIZE").toString());
					if(null != pk && !"".equals(pk) &&pk.toLowerCase().equals(map.get("COLUMN_NAME").toString().toLowerCase())){
						xt_Generator_Table_Column.setCOLUMN_KEY("PRI");
					}
					xt_Generator_Table_ColumnList.add(xt_Generator_Table_Column);
				}
			}
			int i = gutil.createCode(xt_Generator_Table_ColumnList, xtGenerator);
//			xtGenerator.setXt_userinfo_id(getXtUid());
			xtGenerator.setXt_generator_time(getSimpleDateFormat());
			if(i > 0){
				xtGenerator.setXt_generator_state("1");
				result = 1;
			}else{
				xtGenerator.setXt_generator_state("2");
				result = 0;
			}
			xtGenerator.setXt_generator_tbname(table_name);
			xtGenerator.setXt_generator_tbcomment(comment);
			result = xtGeneratorDao.addXtGenerator(xtGenerator);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getCause().getMessage(),e.getCause());
		}
		return result;
	}
	
	/**
	 * 删除
	 * @param condition
	 */
	public int delXtGenerator(Map<String,Object> condition){
		int result = 0;
		try {
			result = xtGeneratorDao.delXtGenerator(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return result;
	}
	/**
	 * 转换数据源配置
	 * @param xtDbinfo
	 * @return
	 */
	private DbInfo convert(XtDbinfo xtDbinfo){
		DbInfo dbInfo = new DbInfo();
		dbInfo.setIp(xtDbinfo.getXt_dbinfoIp());
		dbInfo.setPort(xtDbinfo.getXt_dbinfoPort());
		dbInfo.setUsername(xtDbinfo.getXt_dbinfoUName());
		dbInfo.setPassword(xtDbinfo.getXt_dbinfoPwd());
		dbInfo.setDatabasetype(xtDbinfo.getXt_dbinfoType());
		dbInfo.setDbname(xtDbinfo.getXt_dbinfoName());
		return dbInfo;
	}
}
